package com.wd.myplacard.view.fragment

import android.graphics.Color
import android.os.Bundle
import android.util.TypedValue
import android.view.Gravity
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.lifecycle.Observer
import com.google.android.material.tabs.TabLayout
import com.wd.current.model.Bean.DataBean
import com.wd.live.base.BaseFragment
import com.wd.module_list.adapter.HotTopAdapter
import com.wd.module_list.adapter.NewcomerTopAdapter
import com.wd.myplacard.R
import com.wd.myplacard.databinding.FragmentPlacardBinding
import com.wd.myplacard.model.animation.DipInTransformer
import com.wd.myplacard.viewmodel.PlacardViewModel


class PlacardFragment : BaseFragment<PlacardViewModel,FragmentPlacardBinding>(R.layout.fragment_placard) {
    var hotTopAdapter= mutableListOf<DataBean.FindLeaderBoardBean.Result.HotTop>()
    var newcomerTopAdapter= mutableListOf<DataBean.FindLeaderBoardBean.Result.NewcomerTop>()
    override fun dataObserver() {

        viewmodel.findLeaderBoardData.observe(this, Observer {
            //==============加载数据前清空数据  ======
            hotTopAdapter.clear()
            newcomerTopAdapter.clear()


            //===============给适配器添加数据===========
            if (!it.result.hotTopList.isEmpty() || !it.result.newcomerTopList.isEmpty()){
                hotTopAdapter.addAll(it.result.hotTopList)
                newcomerTopAdapter.addAll(it.result.newcomerTopList)
            }

           viewmodel.tabPositionIndex.observe(this){
                //============Tab与列表联动展示数据==========
                if (it==0){
                    binding.vpList.adapter=context?.let { HotTopAdapter(it,hotTopAdapter) }
                }else if (it==1){
                    binding.vpList.adapter=context?.let { NewcomerTopAdapter(it,newcomerTopAdapter) }
                }
            }



            //=============通过Tab选择改变监听设置下标==============
            binding.rankTab.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                override fun onTabSelected(tab: TabLayout.Tab) {
                    tab.position?.let { viewmodel.tabPosition(it) }
                    setTab(tab,tab.isSelected)
                }

                override fun onTabUnselected(tab: TabLayout.Tab) {
                    setTab(tab,tab.isSelected)
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {

                }

            })
            //==========默认选中最佳新人榜==========
            binding.rankTab.getTabAt(1)?.select()
        })


    }

    override fun initViews() {
        //创建榜单导航
        val titles= arrayOf("最热主播榜","最佳新人榜")
        //设置Tab样式
        for (i in titles){
            binding.rankTab.addTab(binding.rankTab.newTab().setText(i))
            //请求榜单数据
            viewmodel.getFindLeaderBoard()
            //预加载防止数据加载不及时出现错误
            binding.vpList.offscreenPageLimit=2
            //给列表添加动画
           binding.vpList.setPageTransformer(true,DipInTransformer())
            viewmodel.tabPosition(1)
        }

    }

    //==================选中切换Tab文字的大小============
    fun setTab(tab: TabLayout.Tab, isSelected:Boolean){
        if (isSelected){
            val textView= TextView(activity)
            tabText(textView,6,tab)


        }else{
            val textView= TextView(activity)
            tabText(textView,5,tab)
        }

    }


    //==========tab文字方法============
    fun tabText(textView: TextView, size:Int, tab: TabLayout.Tab){
        val selectedSize=
            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,size.toFloat(),resources.displayMetrics)
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,selectedSize)
        //==================居中
        textView.gravity= Gravity.CENTER
        //===============文字颜色
        textView.setTextColor(Color.BLACK)
        //============把tab文字赋值给textView
        textView.text=tab!!.text
        tab.customView=textView
    }




}